/**
 * linruiyan @2015-10-8 重新规划 。命名reui，重构整个默认UI
 */
var reui ={
	//基类
	base:{
		init:function(){}
	},

	//对象句柄
	handles:[],


	
	
	//基础所有的插件需要继承
	ext:function(name,obj){
		obj.extend = this.base;
		return this.plugins[name] = obj;
	},
	//初始化
	init:function(){

		for(var i in this.plugins){
			this.plugins[i].init();
		}
	},
	plugins:{}
};



//----------------------------------------------------------------



/**
 *日历工具 
 */
var calendar={
	obj:null,
	main:null,
	
	currentYear:null,
	currentMonth:null,
	currentDay:null,

	preYear:null,
	preMonth:null,
	
	nextYear:null,
	nextMonth:null,
	
	dayList:null,
	
	init:function(){

		this.main = $("<div />").addClass("n-calendar-pad")
			.append(
				$("<dt />").addClass("calendar-head")
				.append(
					$("<div />").addClass("calendar-date")
						.append($("<a />").addClass("calendar-year-pre").text("<").click(function(){--calendar.currentYear;calendar.fresh();}))
						.append($("<span />").addClass("calendar-year").text("2012"))
						.append($("<a />").addClass("calendar-year-next").text(">").click(function(){++calendar.currentYear;calendar.fresh();}))
						.append($("<a />").addClass("calendar-month-pre").text("<").click(function(){
															if(calendar.currentMonth>1){
																--calendar.currentMonth;
															}else{
																calendar.currentMonth=12;
																--calendar.currentYear;
															}
															calendar.fresh();
														})
													)
						.append($("<span />").addClass("calendar-month").text("01"))
						.append($("<a />").addClass("calendar-month-next").text(">").click(function(){
															if(calendar.currentMonth<12){
																++calendar.currentMonth;
															}else{
																calendar.currentMonth=1;
																++calendar.currentYear;
															}
															calendar.fresh();
														})
													)
				)
				.append($("<div />").addClass("calendar-week")
					.append($("<span />").text("一"))
					.append($("<span />").text("二"))
					.append($("<span />").text("三"))
					.append($("<span />").text("四"))
					.append($("<span />").text("五"))
					.append($("<span />").text("六"))
					.append($("<span />").text("日"))
				)
			)
			.append(
				$("<dd />").addClass("calendar-day")					
			)
			.hide()
			.appendTo('body');
				

		$(".n-calendar").mousedown(function(){
	
			var ymd = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec($(this).val());
			if(ymd!=null){
				calendar.currentYear = ymd[1];
				calendar.currentMonth = parseInt(ymd[2]);
				calendar.currentDay = ymd[3];
			}
			calendar.under(this);
			// alert(1)
			// alert(calendar.main.html())
		});
		$(calendar.main).mouseover(function(){
			$(calendar.main).stop().show();
		});
		$(calendar.main).mouseout(function(){
			calendar.close();
		});			
	},

	close:function(){
		//linruiyan@2012-10-7 fadeTo只是把层透明了。而不会display:none，导致关闭后还能得到焦点，影响底层空间获取焦点
		//this.main.fadeTo("slow",0);
		
		this.main.hide();
	},
	select:function(){},
	
	preYear:function(){},
	nextYear:function(){},
	preMonth:function(){},
	nextYear:function(){},
	
	fresh:function(){
		if(!calendar.currentYear || !calendar.currentMonth){
			var today = new Date();
			calendar.currentYear = today.getUTCFullYear();
			calendar.currentMonth = today.getUTCMonth()+1;
			calendar.currentDay = today.getUTCDate();			 
		}
		this.main.find('.calendar-year').html(calendar.currentYear);
		this.main.find('.calendar-month').html(calendar.currentMonth);
		
		this.main.find("dd").html('');
		//linruiyan@2012-10-8 统一按字面上的数字为月数。参数自己减一来处理
		var day = new Date(calendar.currentYear,calendar.currentMonth,0);
		var firtday = new Date(calendar.currentYear,calendar.currentMonth-1,1).getUTCDay()+1;
		//alert(firtday);
		//linruiyan@2012-10-8 如果当月的1号是星期一，那么将跳过下面的循环。i不被初始化。导致在循环高亮周末时失效。
		//						所以将i的初始化拿到外面来。
		var i=1;
		if(firtday<=7){
			for(;i<firtday;i++){
				this.main.find("dd").append(($("<span />").html(i)));
			}
		}
		var w=i;

		var totalday = day.getDate();
		for(var i=1;i<=totalday;i++){
			
			var item = $("<a />").html(i);
			if(i==this.currentDay){
				item.addClass("current");
			}
			w = ++w%7;
			if(1==w || 0==w){
				item.addClass("weekend");
			}
			this.main.find("dd").append(item);				
		}
		this.main.find("dd a").click(function(){
			$(calendar.main.find("dd a.current").removeClass("current"));
			$(this).addClass("current");
			
			var month = '00'+$(calendar.main).find('.calendar-month').html();
			var day = '00'+$(this).html();
			$(calendar.obj).val($(calendar.main).find('.calendar-year').html()+'-'+month.substr(-2)+'-'+day.substr(-2));
			$(calendar.obj).trigger('change');//选择日期后会自动触发change事件

		});
		this.main.stop(true,false).show();
	},
	under:function(obj){
		var popHandle = $(obj).parents('.ply-html');
		var offset = $(obj).position();
		if(popHandle.length){
			this.main.css({"zIndex":popHandle.css("zIndex")+1});
			ui.closeCallback = function(){calendar.close();};
			offset.left = offset.left + parseInt(popHandle.css('left'))+6;
			offset.top = offset.top + parseInt(popHandle.css('top'))+8;
		}else{
			
		}
		
		this.obj = obj;
		this.main.stop();
		this.fresh();
		$(this.main).css({left:offset.left-106,top:offset.top+4+$(obj).outerHeight()-1});



		//$(this.main).css({left:offset.left-118,top:offset.top+4+$(obj).outerHeight()-parseInt($(obj).css("border-width"))});

	}
};



reui.ext('calendar',calendar);





/**
 *表格工具 
 */
var table = {
	init:function(){
		//alert(2);
		$(".n-table").each(function(){
			if($(this).hasClass("n-init")){
				return ;
			}
			$(this).addClass("n-init");

			var line = 1;
			$(this).find("tbody tr").each(function(){
				$(this).hover(function(){
					$(this).addClass("bright");
				},function(){
					$(this).removeClass("bright");
				});
				if(1==line){
					$(this).addClass("odd");
				}else{
					$(this).addClass("even");
				}
				line = 1-line;
				
				
			});
		});
	},
	saveExcel:function(sheet,obj){
		var form = $("<form>");
		form.attr("method",'POST')
			.attr("action",'/phpexcel/create.html')
			.attr("target","pajax");
		var sheetInput = $("<input>").attr("name","sheet").attr("value",sheet);	
		form.append(sheetInput);
	
	
	
	
		var i=0,j=0;
	
		$(obj).find("tr").each(function(){
			
			j = 0;
			$(this).children().each(function(){
				if(!$(this).hasClass('excel-ignore')){
	
					$("<input>")
						.attr("name","data["+i+"]["+j+"]")
						.attr("value",$(this)[0].tagName+'T&V'+$(this).text())
						.appendTo(form);	
				}
				j++;
			});
	
			i++;
		});
	
	
	
		form.submit();
	}
};

reui.ext('table',table);



/**
 * 搜索框工具
 */
var search = {
	init:function(){
		$('.n-search').each(function(){
			if($(this).hasClass("n-init")){
				return ;
			}
			$(this).addClass("n-init");
			
			var tips = $(this).attr('tips')?$(this).attr('tips'):'请输入';
			
			$(this).parents('form').submit(function(){
				$(this).find(".n-search").each(function(){
					if($(this).val()==$(this).attr('tips')){
						$(this).val('');
					}
				});
			});
			
	
			if(''==$(this).val()){
				$(this).val(tips).removeClass("n-search-input");
			}
	
			$(this).focusin(function(){
				if($(this).val()==tips){
					$(this)
						.val('')
						.removeClass("n-search-input");
				}else{
					$(this)
						.addClass("n-search-input");
				}
	
			});
			//linruiyan@2012-10-7 当输入时，只要不是为空，那么就会设置为主字体颜色
			$(this).keyup(function(){
				if($(this).val()==tips){
					$(this)
						.removeClass("n-search-input");
				}else{
					$(this)
						.addClass("n-search-input");
				}
			});
			$(this).focusout(function(){
				if(''==$(this).val()){
					$(this)
						.val(tips)
						.removeClass("n-search-input");
				}else{
					$(this)
						.addClass("n-search-input");
				}
			});
		});
	}
};
reui.ext('search',search);





/**
 *拖拽框 
 */

var dropli = {
	handle:{},
	project:{},
	data:{},
	init:function(){
	
		for(var i in this.data){
			// alert(this.data[i].id);
			//alert("<li>"+this.data[i].title+"</li>");
			$('#n-dropli').append(this.createItem('【'+this.data[i].pname+'】'+this.data[i].name,this.data[i].id));
			//$('#n-dropli').append("<li>"+this.data[i].title+"</li>");
		}
		this.handle = $('#n-dropli');
		$('#n-dropli').height($('#n-dropli').height());//高度不固定，最下面一个分类会出现屏幕抖动问题
	},
	createItem:function(title,val){
		return $("<li>").text(title)
				.attr('val',val)
				.addClass(val?'private-cate':'common-cate')
				.mousedown(function(e){
					$(this)
							.addClass("refix")
							.addClass("moveing")
					
							//.css({position:'absolute'})
							// .css({'zIndex':dropli.handle.css("zIndex")+1})
// alert(dropli.handle.find("li").length)
					var lastY = e.originalEvent.y || e.originalEvent.layerY || 0;
					
					$(this)
						.mousemove(function(e) { 
							var xx = e.originalEvent.x || e.originalEvent.layerX || 0; 
							var yy = e.originalEvent.y || e.originalEvent.layerY || 0;
							
							// alert(dropli.handle.offset()['top'])
							var offset = yy+$(document).scrollTop();
							// alert(offset);
							$(this).offset({top:offset-10});
							//顶部有面包屑，需要空开
							var replaceItemSeq = Math.ceil((offset-dropli.handle.offset()['top'])/24)-1;
							var itemLen = dropli.handle.find("li").length;
							if(replaceItemSeq>=itemLen){
								replaceItemSeq = itemLen-1;
							}
							// $("#info").html(replaceItemSeq)
							var replaceItem = $(dropli.handle.find("li")[replaceItemSeq]);
							if($(this).attr("val")!=replaceItem.attr("val")){
								//alert(1);
								var newitem = replaceItem.clone(true);
								// $("#info").html(yy%22)
								
								if(yy>lastY){
									$(this).before(newitem);
								}else{
									$(this).after(newitem);	
								}
								lastY = yy;
								
								replaceItem.detach();
							}
						})

				})
				.mouseup(function(){
					$(this)
							.removeClass("moveing")
							.css({top:'auto',left:'auto'})
					$(this).unbind('mousemove');
				})

				.mouseout(function(){
					$(this)
							.mouseup();
				});
	
	}

	
};


reui.ext('dropli',dropli);




function selector(obj,cb){
	
	var that = this;
	var main = $("<ul>")
					.addClass("selector-list")
					.appendTo("body")
					.hide()
					.mouseover(function(){
						lostfocus = 'N';
					})
					.mouseout(function(){
						lostfocus = 'Y';
					});
	

	var input = $(obj)
					.attr("autocomplete","off")
					.keyup(function(e){
						if(-1!=$.inArray(e.keyCode,[37,39])){
							
						}else if(e.keyCode==38){
							preSelected('up');
						}else if(e.keyCode==40){
							preSelected('down');
						}else if(e.keyCode==13){
							var preItem = main.find(".pre-selected");
							if(0<preItem.length){
								preItem.click();
							}
						}else{
							search($(this).val());
							
						}
					})
					.focusout(function(){
						//alert(lostfocus);
						if('Y'==lostfocus){
							main.hide();
						}
					});

	var lostfocus = 'Y';
	var autoclose = 'Y'==input.attr("autoclose")?true:false;
	
	var url = input.attr('url');
	
	var disabled=[],ignore=[],only=[];
	
	this.toDisabled=function(list){disabled = $.merge(disabled,list)};
	this.toIgnore=function(list){ignore = $.merge(ignore,list)};
	this.toOnly=function(list){only = $.merge(only,list)};
	
	this.init=function(ids){
		if(''!=ids){
			$.getJSON(url,{ids:ids},function(data){
				$(data).each(function(){
					var i = $("<div>").data("data",this)
					that.callback(i);
					disabled.push(this.id);
				});
			});
		}		
	};
	
	function selected(obj){
		$(obj).click(function(){
			if(!$(obj).hasClass("disabled")){
				that.callback(this);
				if(autoclose){
					main.hide();
					input.val('');
				}
				disabled.push($(obj).data('data').id);
				/**
				var d = [];
				d.push($(obj).data('data').id);
				that.toDisabled(d);
				 */
				input.focus();
			}
		});
	}	

	function preSelected(direct){
		var current = main.find(".pre-selected")
		var nextItem = null;
		if('up'==direct){
			var nextItem = current.prev();
			if(0==current.length || 0==nextItem.length){
				nextItem = main.find("li:last");
			}
		}else if('down'==direct){
			var nextItem = current.next();
			if(0==current.length && 0==nextItem.length){
				nextItem = main.find("li:first");
			}
		}else{
			nextItem = direct;
		}
		
		main.find(".pre-selected").removeClass("pre-selected");
		nextItem.addClass("pre-selected");
		//console.log(input.attr('id'));
		input.val(nextItem.text());
	}

	function getState(id){
		ppid = parseInt(id);
		if(-1!=$.inArray(id,only)){
			return 'only';
		}else if(-1!=$.inArray(id,ignore)){
			return 'ignore';
		}else if(-1!=$.inArray(id,disabled)){
			return 'disabled';	
		}else{
		}
		return 'normal';
	}
	function search(key){
		if(''==key){
			fresh([]);
		}else{
			$.getJSON(url,{key:key,only:only,ignore:ignore},function(data){
				fresh(data);
			});
		}
	}
	function fresh(data){
		
		main.html('');
		
		if(0==data.length){
			main.hide();
		}else{
			$(data).each(function(){
				var item = this
				var state = getState(item.id);
				if(-1!=$.inArray(state,['normal','disabled'])){
					var itemNode = itemHtml(item,state);
					itemNode.data("data",item)
					itemNode.mouseover(function(){
						preSelected($(this))
					});
					selected(itemNode);
					main.append(itemNode);
					
				}
			});
			main.show();
			
			var offset = input.offset();
			offset.top = offset.top+input.height();
			main.offset(offset)
			
		}
	}
	function itemHtml(item,state){
		var li = $("<li>")
						//.addClass(state)   //如何pop指定数组的值没有合适的方案。所以暂时去掉disable功能
						.mouseover(function(){
							preSelected($(this))
						})
		$("<span>")
				//.attr("href","javascript:void(0)")
				.text(item.name)
				//.addClass('online-'+isonline)
				.appendTo(li);
		return li;
	}

	//function callback(){}
//alert(input.attr("callback"));
	this.callback= undefined==input.attr("callback")?function(obj){}:eval(input.attr("callback"))
	//var callback = eval(input.attr("callback"))
	//this.callback = cb;
	

};


/**
 *鼠标滑过时出现工具栏下过 
 */
var hoverToolsBar = {
	
	init:function(){
		$(".n-hover-toolbar").each(function(){
			if($(this).hasClass("n-init")){
				return ;
			}
			$(this).addClass("n-init");
			
			
			var that = this;
			$(this)
				.hide()
				.parent()
					.css({position:"relative"})
					.hover(function(){
							$(that).show();
						},function(){
							if($(this).find('.n-hover-toolbar .n-focus').length==0){
								$(that).hide();
							}
						});
		});
	}
	
};

reui.ext('hoverToolsBar',hoverToolsBar);




/**
 *linruiyan@2014年11月20日
 * js前端内容检索 
 */

var searchLi = {
	
	handle:null,
	selector:'',
	init:function(){
		this.handle = $("#n-search-li");
		this.selector = this.handle.attr('selector');
		this.handle
			.keyup(function(){
				searchLi.search();
			})
			.change(function(){
				searchLi.search();
			})
			
	},
	search:function(){
		
		var kw = this.handle.val();
		$(this.selector + " li")
					.removeClass('n-show')	
					.removeClass('n-hide');	
		if(kw!=''){
			$(this.selector + " li")
					.addClass('n-hide');	
			$(this.selector + " li:contains('"+kw+"')")
										.removeClass('n-hide')
										.addClass('n-show')
		}
	}
}
reui.ext('searchLi',searchLi);




$(document).ready(function(){
	/*linruiyan@2014年11月20日 重写contains*/
	jQuery.expr[':'].contains = function(a, i, m) {
		return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
	};

});












var tab = {
	init:function(){
		$(".n-tab").each(function(){
			if($(this).hasClass("n-init")){
				return ;
			}
			$(this).addClass("n-init");
			
			
			var more = $("<div>")
							.addClass("n-tab-more")
							.addClass("n-right");
			var moreTab = $("<a>")
								.addClass("more")
								.text("更多");
			more.append(moreTab);
			more.append("<div>");
			
			var list = $("<div>")
							.addClass("n-tab-list")
							.wrapInner($(this).find("a"));
			$(this).append(more);
			$(this).append(list);
					
	
	
	
	
			var moreWidth = 200;
		
			var besides = 650;
			var totalWidth = 0;
			var maxWidth = 0;
			list.find("a").each(function(){
				var itemWidth = $(this).outerWidth();
				besides -= itemWidth;
				maxWidth = itemWidth>maxWidth?itemWidth:maxWidth;
				if(besides < 0){
					$(this).appendTo(".n-tab-more div");
					//如果出现当前标签在more里面，那么就将“更多”改为当前标签名
					if($(this).hasClass("current")){
						moreTab.text($(this).text());
					}
				}
			});
	
			if(besides>0)moreTab.hide();
				else if(besides>-500)moreWidth = 100;
				else if(besides>-1000)moreWidth = 200;
				else if(besides>-2000)moreWidth = 300;
				else if(besides>-3000)moreWidth = 400;
				else moreWidth = 500;
		
			moreWidth = moreWidth<maxWidth?maxWidth:moreWidth;
			
			more.hover(function(){
		
				more.find("div")
						.width(moreWidth)
						.show();
			},function(){
				more.find("div").hide();
			});
	
		});

	}
};

reui.ext('tab',tab);


var select = {
	init:function(){
		$("select").each(function(){
			select.create(this);
		});
	},
	create:function(obj){
	
		var that = $(obj);
		var id = $(obj).attr('id');
		
		//如果元素中未定义id，那么就采用name来识别身份
		//如果name相同，则无奈了
		if(!id){
			id = $(obj).attr('name');
		}
		
		id += '-shadow';
		var handle = $('#'+id);
		if(!handle.length){
			handle = $("<dl><dt></dt><dd></dd></dl>")
								.attr('id',id)
								.addClass('n-select')
								.insertAfter($(obj))
								.hover(function(){
									$(this).find('dd').show();
								},function(){
									$(this).find('dd').hide();
								});
			$(handle).find('dt').text('请选择');
		}else{
			$(handle).find('dd').empty();
		}
		var i = 0;
		$(obj).find("option").each(function(){
			var item = $("<div>")
						.attr("i",i)
						.text($(this).text())
						.click(function(){
							//alert(parseInt($(this).attr('i')))
							$(that).find("option")[parseInt($(this).attr('i'))].selected = true;
							//console.log($(that).find(".selected"))
							$(handle).find('dt').text($(this).text());
							$(that).trigger('change');
						});
		
			handle.find('dd').append(item);
			//console.log($(this).attr('selected'))
			if(i==0 || $(this).attr('selected'))item.click();
	
			i++;
		});
	}
};
reui.ext('select',select);





/**
 *卡片区域排版 
 */
var card = {
	
	init:function(){
		var len = $(".n-card").length;
		for(i=2;i<len;){
			$(".n-card").eq(i).addClass('n-card-end');
			i+=3;
		}
	}
};



/**
 * 复选框 
 */
var checkbox = {
	init:function(){
		$("input[type=checkbox]").each(function(){
			if($(this).hasClass("n-init")){
				return ;
			}
			$(this).addClass("n-init");
			
			
			var id = $(this).attr('name')+':'+$(this).val();
			var that = this;
			var vCheckbox = $("<a><span>✔</span></a>")
						.addClass('n-checkbox')
						.attr('id',id)
						.insertAfter($(this))
						.click(function(){
							console.log(that.checked)
							if(that.checked){
								that.checked = false;
								$(this).removeClass('checked');
								
								//重选时，除了重置that状态，this状态，还需要触发一个事件，让外部可捕获
								//认为checked状态的变化也是一种改变，所以触发了change事件
								$(that).trigger('change');
							}else{
								that.checked = true;
								$(this).addClass('checked');
								$(that).trigger('change');
							}
						})
						.bind('toggleClick',function(){
							if(that.checked){
								$(this).addClass('checked');
							}else{
								$(this).removeClass('checked');
								
							}
						});
	
			//在处理全选/反选中，通过虚拟checkbox选中来改变input
			//input的改变来改变其他所有的input
			//其他所有的input的改变来改变虚拟checkbox
			$(that).change(function(){
				vCheckbox.trigger('toggleClick');
			});
	
		});
	
	
		//复选框，全选
		$(".n-checkbox-all").change(function() {
			if($(this).hasClass("n-init")){
				return ;
			}
			$(this).addClass("n-init");
			
			var gc = $(this).attr("groupclass");
			if(this.checked) {
				$("."+gc).each(function() {
					this.checked = true;
					$(this).trigger('change');
				});
			}
			else {
				$("."+gc).each(function() {
					this.checked = false;
					$(this).trigger('change');
				});
			}
		});
	}
};

reui.ext('checkbox',checkbox);





var ui = (!!parent.ui)?parent.ui:{
		
		id:'pop',
		
		handle:null,
		sunblind:null,
		main:null,
		iframe:null,
		data:null,
		hbox:null,		//隐藏的盒子，存放所有pop
		lasturl:null,
		currenturl:null,
		//创建一个带有工具栏，状态栏的div
		init:function(){
			//linruiyan@2012-10-7 纠正初始化被重复多次执行，导致创建了多个id="pop"
			if(this.main){
				return false;
			}
			//初始化主板
			$('body').append('<div id="'+this.id+'"></div>');
			this.handle = $('#'+this.id);
			
			
			
			//初始化百叶窗
			this.sunblind = $('<div />').
										addClass('sunblind').
										appendTo(this.handle).
										//width($('body').width()).
										height($(document).height()).
										append('<iframe style="width:98%;border:0px;height:100%;filter:alpha(opacity=0);-moz-opacity:0"></iframe>');
			
			//初始化弹窗框架
			this.main = $('<div />').addClass('ply-html').appendTo(this.handle);
			var bar = $('<div />').addClass('ply-bar');
			$('<a />').addClass('ply-close').html('<img src="/skin/img/bg09.gif">').click(ui.close).appendTo(bar);
			$('<span />').addClass('ply-title').html('窗口').appendTo(bar);
			bar.prependTo(this.main);
			
			$('<div />').addClass('ply-body').appendTo(this.main);
			$('<div />').addClass('ply-footer').appendTo(this.main);
			
			this.hbox = $('<div />').addClass('ply-hidden').appendTo(this.main);

			//初始化伪ajax的iframe
			this.iframe = $('<iframe />').attr('name','pajax').attr('id','ply-iframe').addClass('ply-iframe').appendTo(this.handle).load(function(){
				ui.fresh();
			});




			$(window).resize(function(){
				//ui.sunblind.width($('body').width());
				//ui.resetPoistion();
			});
			
			$(window).scroll(function(){
				//ui.resetPoistion();
			});
		
			$(".pop").each(function(){
				ui.hbox.append(this);
			});
		},
		
		submit:function(obj){
			$(obj).attr('target','pajax');
			
		},
		//linruiyan@2013年5月13日 添加了一个获取html
		getPajax:function(url){
			this.iframe.attr("src",url);
		},
		
		pop:function(obj){
			//alert(obj.attr("innerHTML"));
			//ui.main.find('.ply-body').append(obj.attr("innerHTML"));
			// ui.replaceHtml(obj);
			ui.main.find('.ply-body').append(obj);
			this.main.show();
			this.sunblind.show();	
			this.resetPoistion();
			//cityShowCallback();
		},
		
		/**
		 * state
		 * data
		 * callback
		 * 
		 * 2013年11月26日 尝试加入一些自动显示标题的功能。但不好搞定。其次不好用。比如：修改的全是修改
		 */
		open:function(url,param){

			this.lasturl = this.currenturl;//上一个访问url为最后url
			this.currenturl = url;//当前url为访问url
			
			this.loadingHtml();
			this.main.show();
			ui.resetPoistion();
			this.sunblind.show();
			if(/\?/.test(url)){
				url += '&cb=?';
			}else{
				url += '?cb=?';
			}
			url +='&type=ajax';

			$.getJSON(url,param,function(data){
				ui.data = data;
				ui.fresh();
			}); 			
		},
		comfirm:function(str,url,param){
			ui.replaceHtml('');
			ui.main.find('.ply-body').append($('<div />').addClass('alert').text(str));
			
			;
			this.setFooter($('<a />')
					.text("确定")
					.attr("href","javascript:void(0);")
					.addClass('button')
					.click(function(){
							ui.loadingHtml();
							ui.open(url, param);
							$(this).unbind("click");
							$(this).css({color:"#999"});
							//alert(1);
						}));
			this.main.show();
			this.sunblind.show();	
			this.resetPoistion();
			
		},
		
		golast:function(){
			//如果弹窗被关闭，那么就不要再次刷新了
			if('block'==$(this.main).css('display')){
				var url = this.lasturl;
				this.lasturl = null;
				if(url==null){
					this.close();
				}else{
					this.open(url);
				}

			}
		},
		fresh:function(){
			
			
			//在第一次加载时，会返回烦人的null
			if(!ui.data)return false;
			
			var data = ui.data;
			if('append'==data.fresh){
				
			}else if('nothing'==data.fresh){
				return false;
			}else{
				ui.replaceHtml('');
				this.main.find('.ply-footer').hide();
			}
			data.msg = "<div>"+data.msg+"</div>";
			$(data.msg).find('.notice').each(function(){
				ui.prependHtml(this);
			});
			$(data.msg).find('.content').each(function(){
				ui.appendHtml(this);
			});
			$(data.msg).find('a').each(function(){
				var obj = $(this);
				
				if('javascript:'!=obj.attr("href").substr(0,11)){
					//alert(obj.attr("href"));
					obj.click = function(){
						alert($(this).attr('href'));
						alert(1);
						return false;
						obj.attr("href","javascript:void(0);");
						//ui.open(obj.attr('href'));
					};
				}
			});
			//return false;
			//下面这段代码会导致js重复执行，在setIntval时会导致混乱
			var script = data.msg.match(/<script[\S\s]*?script>/gi);
			if(script){
				$.each(script,function(){
					//alert(this.toString());
					//$('body').append(this.toString());
				});					
			}

			this.main.find('form').submit(function(){
				
				var url = $(this).attr('action');
				url += /\?/.test(url)?'&type=iframe':'?type=iframe';
				
				$(this).attr('action',url);
				ui.submit(this);
			});
			//linruiyan@2013年2月19日 如果弹窗中有图片，在图片加载完了之后再次调整弹窗尺寸和
			this.main.find("img").load(function(){
				ui.resetPoistion();
			});
			this.resetPoistion();
			reui.init();
		},
		closeCallback:function(){},
		close:function(){
			ui.main.hide();
			ui.sunblind.hide();
			ui.closeCallback();
			//将pop数据返回hbox中
			ui.main.find(".pop").each(function(){
				ui.hbox.append(this);
			});
			//linruiyan@2012年9月5日 如果嵌套iframe，ui会持续存在。iframe调用弹窗后closeCallback会继续有效。需要重置为空
			ui.closeCallback = function(){};
			
			//linruiyan@2013年11月28日 在窗口关闭后，不能存在“返回”处理了。避免陷入“死循环”返回
			ui.lasturl = null;
		},

		setTitle:function(title){
			this.main.find('.ply-title').text(title);
		},
		setFooter:function(html){
			
			this.main.find('.ply-footer').html(html).show();
//			this.main.find('.ply-footer');
		},

		
		
		replaceHtml:function(html){
			ui.main.find('.ply-body').html(html);
		},
		appendHtml:function(html){
			ui.main.find('.ply-body').append(html);
		},
		prependHtml:function(html){
			ui.main.find('.ply-body').prepend(html);
		},
		
		
		loadingHtml:function(){

			ui.replaceHtml('<div>加载中...</div>');
			this.main.show();
			this.sunblind.show();	
			this.resetPoistion();

		},
		resetPoistion:function(){
			var top = 100;
			if(this.main.height()>=$(window).height()-100){
				this.main.height($(window).height()-100);
				top = 50;
			}
			this.main.css({left:($(document).width()-this.main.width())/2,top:$(document).scrollTop()+top});
			this.main.find('.ply-body').height('auto');	//重置ply-body的自然高度
			var barHeight = this.main.find('.ply-bar').outerHeight();
			var bodyHeight = this.main.find('.ply-body').outerHeight();//获取ply-body的自然高度
			var footerHeight = this.main.find('.ply-footer').outerHeight();//获取ply-footer的自然高度,footer带有padding，需要outerHeight才能准确计算出真正的高度
			//如果脚步隐藏了。那么就按0来段。
			if('none'==this.main.find('.ply-footer').css("display")){
				footerHeight = 0;
			}
			
			var maxHeight = bodyHeight + footerHeight+barHeight;
			//如果ply-body的自然高度大于窗口除去上下留白高度。那么约束在最大高度（window-200）
			var autoHeight = $(window).height()-100; 
			//alert(autoHeight);
			if(maxHeight>autoHeight){
				this.main.find('.ply-body').height(autoHeight-footerHeight-barHeight-20);
				bodyHeight = autoHeight-footerHeight-barHeight;
			}
			
			this.main.height("auto");
		}

};

reui.ext('ui',ui);





/*-------------------------------------------------*/

jQuery.extend(reui);
$(document).ready(function(){
	$.init();
});
